package com.sky.aspect;

import com.sky.annotion.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Slf4j
@Component
@Aspect
public class AutoFillAspect {

    /**
     * 切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) &&@annotation(com.sky.annotion.AutoFill)")
    public void autoFillPointCut() {
    }

    /*
     * 前置通知：在连接点之前执行的通知
     * */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段自动填充...");
        //获得当前被拦截方法上的数据库操作类型
        MethodSignature autoFill = (MethodSignature) joinPoint.getSignature();
        OperationType operationType = autoFill.getMethod().getAnnotation(AutoFill.class).value();
        //获得到当前被拦截的方法参数
        Object[] args = joinPoint.getArgs();
        if (args.length == 0) {
            return;
        }
        Object entity = args[0];
        //根据不同的数据库操作类型，为对应的表字段赋值
        if (operationType == OperationType.INSERT){
            try {
                LocalDateTime now = LocalDateTime.now();
                Long currentId = BaseContext.getCurrentId();
                Method setCreatTime= entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreatUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setCreatTime.invoke(entity, now);
                setCreatUser.invoke(entity, currentId);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, currentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else if (operationType == OperationType.UPDATE) {
            LocalDateTime now = LocalDateTime.now();
            Long currentId = BaseContext.getCurrentId();
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, currentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }


        }

//        switch (operationType) {
//            case INSERT:
//                this.fillInsert(entity);
//                break;
//            case UPDATE:
//                this.fillUpdate(entity);
//                break;
//        }
//    }
//
//    private void fillUpdate(Object object) {
//        //赋值
//        log.info("公共字段自动填充[update]...");
//        LocalDateTime now = LocalDateTime.now();
//        try {
//            object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class).invoke(object,now);
//            object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class).invoke(object,BaseContext.getCurrentId());
//            log.info("公共字段自动填充[update]完成");
//        } catch (IllegalAccessException e) {
//            throw new RuntimeException(e);
//        } catch (InvocationTargetException e) {
//            throw new RuntimeException(e);
//        } catch (NoSuchMethodException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    private void fillInsert(Object object) {
//        //赋值
//        log.info("公共字段自动填充[insert]...");
//        try {
//            object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class).invoke(object, LocalDateTime.now());
//            object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class).invoke(object, LocalDateTime.now());
//            object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class).invoke(object, BaseContext.getCurrentId());
//            object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class).invoke(object, BaseContext.getCurrentId());
//            log.info("公共字段自动填充[insert]完成");
//        } catch (InvocationTargetException e) {
//            throw new RuntimeException(e);
//        } catch (IllegalAccessException e) {
//            throw new RuntimeException(e);
//        } catch (NoSuchMethodException e) {
//            throw new RuntimeException(e);
//        }
//    }
}}
